var boxId;
var loadSuccess = false;
var vue = {};
var data = {};
var form = {};
var props={
		converter:{
			on:{
				status:"on",
				clz:"glyphicon glyphicon-option-horizontal"
			},
			off:{
				status:"off",
				clz:"glyphicon glyphicon-option-vertical"
			}
		},
		menu:{
			close:{
				status:"close",
				clz:"glyphicon glyphicon-chevron-right"
			},
			open:{
				status:"open",
				clz:"glyphicon glyphicon-chevron-down"
			}
		}
};
vue.initialize = function() {
	request();
	resolveSwitch("on");
	form.oldPassword="";
	form.newPassword="";
	form.confirmPassword="";
	data.message="";
	data.form = form;
	new Vue({
		el : ".wrap",
		data : data,
		methods : vue
	});
	Vue.nextTick(function() {
		if(loadSuccess){
			bindEvent();
			$(".wrap").show();
			$(".menu-title")[0].click();
		}
	});
};
vue.change=function(){
	resolveSwitch(data.converter.status,"change");
};
vue.logout=function(){
	layer.confirm('确定注销？', {
		title : "提示",
		icon : 3
	}, function(i) {
		layer.close(i);
		var index = util.load();
		util.post({
			url : "user/logout",
			success:function(){
				window.top.location.href = "login";
				return;
			},
			complete:function(){
				layer.close(index);
			}
		});
	});
};
vue.popModifyBox=function(){
	var $div = $(".pwd-div");
	form.oldPassword="";
	form.newPassword="";
	form.confirmPassword="";
	data.message="";
	boxId = layer.open({
		type : 1,
		shift : 2,
		area : [ '320px', '300px' ],
		title : "修改密码",
		content : $div
	});
};
vue.modifyPwd=function(){
	modifyPwd();
};
vue.closeModifyBox=function(){
	layer.close(boxId);
};
function bindEvent(){
	$(".menu-title").click(function(){
		var $this=$(this);
		var $items=$this.next();
		var i=$this.data("index");
		$items.slideToggle("fast",resolveMenu(i));
	});
};
function resolveSwitch(status,type){
	var _switch={};
	if(type=='change'){
		if(status=="on"){
			_switch=props.converter["off"];
			$(".menu-container").css({"width":"30px"})
			$(".content-container").css({"left":"30px"})
			$(".menu-container .list-group").hide();
		}else{
			_switch=props.converter["on"];
			$(".menu-container").css({"width":"180px"})
			$(".content-container").css({"left":"180px"})
			$(".menu-container .list-group").show();
		}
	}else{
		_switch=props.converter[status];
	}
	data.converter=_switch;
};
function resolveMenu(i){
	var menus=data.menus;
	var curMenu=menus[i];
	var status=curMenu.iconStatus;
	if(status=='close'){
		for(var j in menus) {
			var _menu=menus[j];
			var _status=_menu.iconStatus;
			if(_status=='open'){
				$(".menu"+j).next().slideUp("fast");
				_menu.iconStatus="close";
				_menu.iconClass=props.menu.close.clz;
			}
		}
		curMenu.iconStatus="open";
		curMenu.iconClass=props.menu.open.clz;
	}else{
		curMenu.iconStatus="close";
		curMenu.iconClass=props.menu.close.clz;
	}
};
function request(){
	var loadId=util.load();
	util.syncPost({
		url : "user/detail",
		success:function(result){
			loadSuccess=true;
			data.user=result;
			processMenus();
		},
		fail:function(code,msg){
			loadSuccess=false;
			util.alert(msg);
		},
		complete:function(){
			layer.close(loadId);
		}
	});
};
function processMenus(){
	var menus=data.user.menu.menus;
	for(var i in menus) {
		var menu=menus[i];
		var subMenus=menu.subMenus;
		menu.iconStatus=props.menu.close.status;
		menu.iconClass=props.menu.close.clz;
		for(var i in subMenus) {
			var subMenu=subMenus[i];
			var url=subMenu.url;
			if($.trim(url)==''){
				subMenu.url="javascript:promptNo();";
			}
		}
	}
	data.menus=menus;
};
function modifyPwd() {
	var $div = $(".pwd-div");
	var $oldPwd = $div.find(".oldPwd");
	var $newPwd = $div.find(".newPwd");
	var $confirmPwd = $div.find(".confirmPwd");
	if ($.trim(form.oldPassword) == '') {
		data.message="请填写原密码！";
		$oldPwd.focus();
		return;
	}
	if ($.trim(form.newPassword) == '') {
		data.message="请填写新密码！";
		$newPwd.focus();
		return;
	}
	if (form.newPassword.length < 6) {
		data.message="新密码长度应大于6位！";
		$newPwd.focus();
		return;
	}
	if (form.confirmPassword != form.newPassword) {
		data.message="两次密码输入不一致！";
		$confirmPwd.focus();
		return;
	}

	var $modifyPwdBtn = $(".pwd-div .modify-pwd-btn");
	var btnHtml = $modifyPwdBtn.html();
	$modifyPwdBtn.html("修改中...");
	$modifyPwdBtn.attr('disabled', "true");
	util.syncPost({
		url : "user/modifyPassword",
		data:form,
		success:function(){
			data.message="";
			layer.close(boxId);
			util.alert("操作成功");
			return;
		},
		fail:function(code,msg){
			data.message=msg;
		},
		complete:function(){
			$modifyPwdBtn.html(btnHtml);
			$modifyPwdBtn.removeAttr("disabled");
		}
	});
};
function promptNo(){
	util.alert("暂未开通！");
}
